// pages/index/panel/oldPanel/oldPanel.js
import {
    Ble
} from "../../../../utils/ble.js"
import {
    Protocol
} from "../../../../utils/protocol"

Page({

    /**
     * Page initial data
     */
    data: {
        // for device
        bleStatus: "",
        macAddress: 0x00FFEECC,
        lamp: false,

        calc: false,
        powerSide: "40vw",
        powerAreaLength: {
            minX: 0,
            maxX: 0,
            minY: 0,
            maxY: 0,
        }, // px

        oldPanelBackground: "#1E1E1E",
        pairBtnBackground: "#4b4b4b",
        powerBackground: "rgba(255,255,255,0)",
        powerInShadow: "rgba(0,0,0,1)", // #868686
        powerOutShadow: "rgba(255,255,255,0)", // #868686
        powerIcon: "/image/icon/power_off.png",
        oldBtnBackground: "#4b4b4b", // "#6588FE",
        oldBtn: [{
            name: "定时",
            icon: "/image/icon/icon_timing_1.png",
        }, {
            name: "倒计时",
            icon: "/image/icon/stopwatch.png"
        }]
    },

    /**
     * Lifecycle function--Called when page load
     */
    onLoad(options) {

    },

    /**
     * Lifecycle function--Called when page is initially rendered
     */
    onReady() {
        // setInterval(() => {
        //     this.displayFresh()
        // }, 800);
    },

    /**
     * Lifecycle function--Called when page show
     */
    onShow() {
        this.setData({
            calc: false
        })
    },

    /**
     * Lifecycle function--Called when page hide
     */
    onHide() {

    },

    /**
     * Lifecycle function--Called when page unload
     */
    onUnload() {

    },

    /**
     * Page event handler function--Called when user drop down
     */
    onPullDownRefresh() {

    },

    /**
     * Called when page reach bottom
     */
    onReachBottom() {

    },

    /**
     * Called when user click on the top right corner to share
     */
    onShareAppMessage() {

    },

    // ================================================ 蓝牙通信相关 ==================================================
    /**
     * 蓝牙通信方法
     * @param {string} bytes 
     */
    sendData(bytes) {
        Ble.server.startAdvertising({
            advertiseRequest: {
                serviceUuids: bytes
            },
            powerLevel: 'high',
            success: (res) => {
                console.log(" advertise message success ")
                this.timer = setTimeout(() => {
                    Ble.server.stopAdvertising({})
                }, 300)
            },
            fail: (error) => {
                console.log(JSON.stringify(error))
            },
            complete: () => {
                console.log(" advertise message complete ")
            }
        })
    },
    /**
     * 配对
     */
    pairDevice(e) {
        let params = new Uint8Array(9)
        params[0] = 0x01
        params[3] = 0xFF
        params[4] = 0xEE
        params[5] = 0xCC
        params[6] = 0xFF
        params[7] = 0xEE
        params[8] = 0xCC
        let bytes = Protocol.encode(0x02, this.data.macAddress, params)
        this.sendData(bytes)
    },
    /**
     * 仅开灯，保留原色温和亮度
     */
    turnOn(e) {
        let params = new Uint8Array(9)
        params[0] = 0x23
        let bytes = Protocol.encode(0x3B, this.data.macAddress, params)
        this.sendData(bytes)
    },
    /**
     * 仅关灯，保留原色温和亮度
     */
    turnOff(e) {
        let params = new Uint8Array(9)
        params[0] = 0x24
        let bytes = Protocol.encode(0x3B, this.data.macAddress, params)
        this.sendData(bytes)
    },

    /**
     * touch funtion - start
     */
    powerStart(e) {
        console.log("start")
        if (this.data.calc == false) {
            var tmpLength = wx.getWindowInfo().windowWidth * (this.data.powerSide.slice(0, this.data.powerSide.length - 2) / 100)
            var left = e.currentTarget.offsetLeft
            var top = e.currentTarget.offsetTop
            var aX = left + tmpLength
            var aY = top + tmpLength
            this.setData({
                powerOutShadow: "rgb(255 ,255 ,255 ,0.5)",
                calc: true,
                powerAreaLength: {
                    minX: left,
                    maxX: aX,
                    minY: top,
                    maxY: aY,
                }
            })
        } else {
            this.setData({
                powerOutShadow: "rgb(255 ,255 ,255 , 0.5)",
            })
        }
    },
    /**
     * touch funtion - start
     */
    powerMove(e) {
        // console.log(e.touches[0])
        var newX = e.touches[0].pageX
        var newY = e.touches[0].pageY

        console.log("moving")
        if (newX > this.data.powerAreaLength.minX && newX < this.data.powerAreaLength.maxX && newY > this.data.powerAreaLength.minY && newY < this.data.powerAreaLength.maxY) {
            console.log("moving-in")
        } else {
            console.log("moving-out")
            if (this.data.powerOutShadow != "rgba(255,255,255,0)") {
                this.setData({
                    powerOutShadow: "rgb(255,255,255,0)",
                })
            }
        }
    },
    /**
     * touch funtion - start
     */
    powerEnd(e) {
        // console.log(e.changedTouches[0])
        console.log("end")
        var newX = e.changedTouches[0].pageX
        var newY = e.changedTouches[0].pageY

        if (newX > this.data.powerAreaLength.minX && newX < this.data.powerAreaLength.maxX && newY > this.data.powerAreaLength.minY && newY < this.data.powerAreaLength.maxY) {
            console.log("end-complete")
            this.powerSwitch()
        } else {
            console.log("end-cancel")
            if (this.data.powerOutShadow != "rgba(255,255,255,0)") {
                this.setData({
                    powerOutShadow: "rgb(255,255,255,0)",
                })
            }
        }
    },
    /**
     * Power - switch
     */
    powerSwitch() {
        this.setData({
            lamp: !this.data.lamp
        })
        this.displayFresh()
        if (this.data.lamp == true) {
            this.turnOn()
        } else {
            this.turnOff()
        }
    },
    
    /**
     * fresh display
     */
    displayFresh() {
        if (this.data.lamp == true) {
            // on
            this.setData({
                oldPanelBackground: "-webkit-linear-gradient(bottom,#445CEF,#00CBFF)",
                pairBtnBackground: "#39CAFF",
                powerBackground: "#ffffff",
                powerInShadow: "rgba(255,255,255,0)",
                powerOutShadow: "rgba(134,134,134,0)",
                powerIcon: "/image/icon/power_on.png",
                oldBtnBackground: "#6588FE",
            })
        } else {
            // off
            this.setData({
                oldPanelBackground: "#1E1E1E",
                pairBtnBackground: "#4b4b4b",
                powerBackground: "rgba(255,255,255,0)",
                powerInShadow: "rgba(0,0,0,1)",
                powerOutShadow: "rgba(255,255,255,0)",
                powerIcon: "/image/icon/power_off.png",
                oldBtnBackground: "#4b4b4b", // "#6588FE",
            })
        }
    },
    /**
     * timer & stopwatch
     */
    test() {
        wx.showToast({
            title: '功能待更新',
            icon: 'error',
            duration: 2000
        })
    }
})